<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$return['post']=array();
$return['paytypes']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
if($_RQ['op']=='main'){
	exi($return);
}
/*******op*******/
set_html('title','我的客户');
if($_RQ['op']=='list'){
	$params=array('peisong_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_peimilk_orderuser',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),array('merchname','logo'));
			$item['goodslist']=$this->model('order')->get_allgoods($item['mid'],$item['merchid']);
            $peiset=$this->pdo_get('peiset',array('order_id'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'merchid'=>$item['merchid']));
            if($peiset['address']){
                $item['address']=json_decode($peiset['address'],true);
            }else{
                $order=pdo_get('jy_weishop_order',array('mid'=>$item['mid'],'merchid'=>$item['merchid'],'ispay'=>1,'is_dingpei'=>1),array('address','sendtype','addressid','is_dingpei'),'id desc');
                $order['address']=json_decode($order['address'],true);
                if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
                    $address=$order['address'];
                }else{
                    $address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
                }
                if(!is_array($address['pcd'])){
                    $address['pcd']=json_decode($address['pcd'],true);
                }
                $item['address']=$address;
            }
            $item['addressinfo']=model('core/member')->get_address_str($item['address']);
		}
	}
	$return['list']=$list;
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	$areas=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $v){
		$users=pdo_getall('jy_peimilk_member',array('area_id'=>$v['id'],'status'=>1),array('mid','title'));
		foreach($users as $uv){
			$peisongs[]=$uv;
		}
	}
	$return['peisetting']=$this->peisetting;
	$return['isopenvoice']=$this->session('isopenvoice')?1:0;
	$return['peisongs']=$peisongs;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['store']=$store;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_receive($order_id);
	exi('恭喜你完成一单！感谢你的努力！');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_weishop_order',array('status'=>'1'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}
//转单
if($_RQ['op']=='change'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$order_id));
	$this->pdo_delete('sendlog',array('uniacid'=>$_SESSION['uniacid'],'buymid'=>$order['mid'],'merchid'=>$order['merchid'],'status'=>array(0,1)));
	if($post['paisong_id']){
		pdo_update('jy_weishop_order',array('peisong_id'=>$post['paisong_id'],'send_status'=>13),array('id'=>$order_id));
		pdo_update('jy_weishop_order',array('peisong_id'=>$post['paisong_id']),array('mid'=>$order['mid'],'merchid'=>$order['merchid'],'status'=>array(1,2)));
		$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid'],$post['paisong_id']);
	}else{
		pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('mid'=>$order['mid'],'merchid'=>$order['merchid'],'status'=>array(1,2)));
		$orderuser=$this->model('model')->up_orderuser($order['mid'],$order['merchid'],'no');
	}
	exi('转单成功！');
}
//员工确认接单
if($_RQ['op']=='jieorder'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_checkget($order_id);
	exi('接单成功！');
}
//报备
if($_RQ['op']=='baobei'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	if($post['baobeitype']==3){
		$msg=$this->baobei_types3[$post['othertype']];
	}else{
		$msg=$this->baobei_types[$post['baobeitype']];
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	model('jy_weishop/order')->add_baobei($order_id,$msg,2,'',$order['merchid'],'jy_merch');
	exi('报备成功！','','jy_peimilk/worker_order/list/');
}
//发起定期配送
if($_RQ['op']=='dingpei1'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$day=$_RQ['day'];
	$key=$day?strtotime($day):0;
	if(!$day || $key>time()){
		exi('当天没有要配送的，只能发起当天的配送任务！','error');
	}
	$this->model('order')->dingpei1($order_id,$day);
	exi('发起配送成功，请在指定时间段完成内配送！');
}
//发起定期配送
if($_RQ['op']=='dingpei2'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->dingpei2($order_id);
	exi('恭喜你完成一单！感谢你的努力！');
}